package hu.szyrt.bug.overlays;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View.OnClickListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;

public class PlacesOverlay extends ItemizedOverlay<PlaceOverlayItem> {
	
	private Drawable defaultMarker;
	private List<PlaceOverlayItem> items;	
	
	private MapView mapView;
	private PlaceAnnotation annotation;
	private long lastShowTime = 0;
	private int lastIndex = 0;
	
	private OnClickListener annotationClick,
							directionFromHereClick,
							directionToHereClick;
	
	private GestureDetector gestureDetector;
	
	public PlacesOverlay(Drawable defaultDrawable) {
		super(boundCenter(defaultDrawable));
		this.defaultMarker = boundCenter(defaultDrawable);
		this.items = new ArrayList<PlaceOverlayItem>();		
		populate();
	}
	
	public void initIfNeed(){
		if(this.annotation == null){
			this.annotation = new PlaceAnnotation(mapView.getContext());	
			this.annotation.setOnClickListener(annotationClick);
			this.annotation.getButton1().setOnClickListener(directionFromHereClick);
			this.annotation.getButton2().setOnClickListener(directionToHereClick);
		}
	}
	
	public void addItemsBulk(List<PlaceOverlayItem> items){
		this.items.addAll(items);
	}
	
	public void addItems(List<PlaceOverlayItem> items){
		this.items.addAll(items);
		populate();
	}
	
	public void setItems(List<PlaceOverlayItem> items) {
		this.items = items;
		populate();
	}
	
	public void populateBulk(){
		populate();
	}

	@Override
	protected PlaceOverlayItem createItem(int i) { return items.get(i); }
	@Override
	public int size() { return items.size(); }

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {	
		super.draw(canvas, mapView, false);
	}
	
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {		
		this.mapView = mapView;
		return super.onTap(p, mapView);
	}
	
	@Override
	protected boolean onTap(int index) {
		if(this.mapView != null)
			showAnnotationAt(index, this.mapView);
		return super.onTap(index);
	}
		 
	private class MyGestureListener extends SimpleOnGestureListener{		
		public boolean onSingleTapConfirmed(MotionEvent e) {
			if(System.currentTimeMillis() - lastShowTime > 800)
				hideAnnotation();
			return super.onSingleTapConfirmed(e);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
		if(gestureDetector != null) gestureDetector.onTouchEvent(ev);
		return super.onTouchEvent(ev, mapView);
	}
	
	
	
	public void hideAnnotation(){
		if(mapView != null)
			mapView.removeView(annotation);
	}
	
	public void showAnnotationAt(int index, MapView mapView){
		if(mapView != null){
			if(this.mapView == null)  		 this.mapView = mapView;			
			if(this.gestureDetector == null) this.gestureDetector = new GestureDetector(mapView.getContext(), new MyGestureListener());
			
			hideAnnotation();
			initIfNeed();
			
			GeoPoint point = items.get(index).getPoint();
			BitmapDrawable marker = (BitmapDrawable) items.get(index).getMarker(0);
					 	   marker = (BitmapDrawable) (marker == null ? defaultMarker : marker);
			String title = items.get(index).getTitle();
			String snippet = items.get(index).getSnippet();
			BitmapDrawable icon = (BitmapDrawable) items.get(index).getIcon();
			
			// eltolás hogy elferjen
			Point animateToPoint = mapView.getProjection().toPixels(point, null);
				  animateToPoint.y -= 80;			
			GeoPoint animateToGeoPoint = mapView.getProjection().fromPixels(animateToPoint.x, animateToPoint.y);
			
					
			mapView.getController().animateTo(animateToGeoPoint);
			
			if((snippet != null && snippet.trim().length() > 0) || (title != null && title.trim().length() > 0)){
				MapView.LayoutParams params = new MapView.LayoutParams(
						LayoutParams.WRAP_CONTENT, 
		                LayoutParams.WRAP_CONTENT, 
		                point,
		                0, 
		                (-1 * marker.getIntrinsicHeight() >> 1) + 20,
		                MapView.LayoutParams.BOTTOM_CENTER);							
								
				annotation.setTitle(Html.fromHtml("<b>" + title + "</b><br />\n<font color='#dddddd'>" + snippet + "</font>"));
				annotation.setIcon(new BitmapDrawable(annotation.getResources(), icon.getBitmap()));
				
				mapView.addView(annotation, params);
				annotation.focus();
			}
			
			lastIndex = index;
			lastShowTime = System.currentTimeMillis();
		}
	}
	
	public int getLastIndex() {
		return lastIndex;
	}
	
	public PlaceOverlayItem getLastItem(){
		return items.get(lastIndex);
	}
	
	public PlaceAnnotation getAnnotation() {
		return annotation;
	}
		
	public void setAnnotationClick(OnClickListener annotationClick) {
		this.annotationClick = annotationClick;
	}
	
	public void setDirectionFromHereClick(OnClickListener directionFromHereClick) {
		this.directionFromHereClick = directionFromHereClick;
	}
	
	public void setDirectionToHereClick(OnClickListener directionToHereClick) {
		this.directionToHereClick = directionToHereClick;
	}
	
}
